package com.dragee.modules.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragee.common.PageUtils;
import com.dragee.constants.CommonConstants;
import com.dragee.modules.work.dao.ShopDao;
import com.dragee.modules.work.dao.ShopProjectDao;
import com.dragee.modules.work.entity.ShopGoodsEntity;
import com.dragee.modules.work.entity.ShopProjectEntity;
import com.dragee.modules.work.entity.ShopProjectGoodsEntity;
import com.dragee.modules.work.po.UserGoodsPo;
import com.dragee.modules.work.service.ShopGoodsService;
import com.dragee.modules.work.service.ShopProjectGoodsService;
import com.dragee.modules.work.service.ShopProjectService;
import com.dragee.utils.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service("shopProjectService")
public class ShopProjectServiceImpl extends ServiceImpl<ShopProjectDao, ShopProjectEntity> implements ShopProjectService {


    @Resource
    private ShopProjectDao shopProjectDao;
    @Autowired
    private ShopGoodsService shopGoodsService;
    @Autowired
    private ShopProjectGoodsService shopProjectGoodsService;

    @Resource
    private ShopDao shopDao;

    private String business = "shopProject";
    /**
    * 列表
    */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int limit = params.get(CommonConstants.LIMIT) == null
                ?CommonConstants.LIMIT_SIZE:Integer.parseInt(params.get(CommonConstants.LIMIT).toString());
        int start = params.get(CommonConstants.START) == null
                ?CommonConstants.START_SIZE:Integer.parseInt(params.get(CommonConstants.START).toString());
        QueryWrapper<ShopProjectEntity> wrapper =
                WrapperUtil.getWrapper(business, params, ShopProjectEntity.class);
        IPage<ShopProjectEntity> page = new Page<>(start, limit);
        page = baseMapper.selectPage(page, wrapper.select());
        for (ShopProjectEntity p : page.getRecords()) {
            List<UserGoodsPo> relationGoods = shopDao.getRelationGoods(p.getId().toString());
            p.setGoods(relationGoods);
        }
        return new PageUtils(page);
    }

    @Override
    public BigDecimal reduceShopGoodsByProject(String projectId) {
        List<ShopProjectGoodsEntity> shopProjectGoods
                = shopProjectGoodsService.list(new QueryWrapper<ShopProjectGoodsEntity>().eq("projectId", projectId));
        // 用品成本
        BigDecimal cost = new BigDecimal(0);
        for (ShopProjectGoodsEntity shopProjectGood : shopProjectGoods) {
            String goodId = shopProjectGood.getGoodId();
            Integer reduceSum = shopProjectGood.getSum();
            ShopGoodsEntity good = shopGoodsService.getById(goodId);
            Integer total = good.getSum();
            if (total > reduceSum) {
                good.setSum(total - reduceSum);
            } else {
                good.setSum(0);
            }
            shopGoodsService.saveOrUpdate(good);
            BigDecimal onceCost = new BigDecimal(good.getCost()).multiply(new BigDecimal(reduceSum));
            cost = cost.add(onceCost);
        }
        return cost;
    }
}